//
//  ShouldReceiveViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/12/13.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class ShouldReceiveViewController: BaseViewController {

    @IBOutlet weak var companyField: UITextField!
    
    @IBOutlet var namesLabel: [UILabel]!
    
    @IBOutlet var countLabels: [UILabel]!
    
    @IBOutlet var months: [UILabel]!
    
    var selectedCompany = ""
    
    var companys: Array<JSON> = []
    
    @IBAction func tenderAction(_ sender: UIButton) {
        
        
    }
    
    @IBAction func saleAction(_ sender: UIButton) {
        
//        let destinate = ProjectDetailChartViewController()
//
//        destinate.candetail = true
//
//        destinate.title = self.lbls[1].text
//
//        destinate.companys = self.companys
//
//        destinate.selectedCompany = self.selectedCompany
//
//        destinate.key = "marketingcontract"
//
//        self.push(destination: destinate)
        
    }
    
    @IBAction func tickAction(_ sender: UIButton) {
        
//        let destinate = ProjectDetailChartViewController()
//
//        destinate.title = self.lbls[2].text
//
//        destinate.companys = self.companys
//
//        destinate.selectedCompany = self.selectedCompany
//
//        destinate.key = "openingInvoiceRecord"
//
//        self.push(destination: destinate)
        
    }
    
    @IBAction func ticketBackAction(_ sender: UIButton) {
//        let destinate = ProjectDetailChartViewController()
//
//        destinate.title = self.lbls[3].text
//
//        destinate.companys = self.companys
//
//        destinate.selectedCompany = self.selectedCompany
//
//        destinate.key = "backCapitalService"
//
//        self.push(destination: destinate)
    }
    
    override var names: Array<String> {
        get{
            return ["GetOrg","Detail"]
        }
    }
    
    override func success(for name: String) -> NetSuccess{
        
        func getOrgSuc(json: JSON)->Void{
            if json.contents?["success"]?.bool == true {
                if let data = json.contents?["data"]?.array {
                    companys = data
                    if companys.count > 0 {
                        let item = companys[0]
                        selectedCompany = item.contents!["code"]!.rawString()!
                        self.companyField.text = item.contents?["name"]?.rawString()
                        self.load(with: "Detail")
                    }
                    
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        func detail(json: JSON)->Void{
            
            
        }
        
        
        func needReceive(json: JSON)->Void{
            
        }
        
        if name == "GetOrg" {
            return getOrgSuc
        } else if name == "JYYSK" {
            return needReceive
        } else {
            return detail
        }
    }
    
    override func param(for name: String) -> Dictionary<String,Any>{
        if name == "GetOrg" {
            return [:]
        } else if name == "Detail" {
            return ["orgCode":self.selectedCompany]
        }
        return [:]
    }
    
    override func path(for name: String) -> String{
        if name == "GetOrg" {
            return GETORG
        } else {
            return ""
        }
    }
    
    override func method(for name: String) -> NetMethod{
        return .get
    }
    
    //响应接口的有效性策略,如果参数不合格，则默认根据String提示
    override func strategy(for name: String) -> (Bool,String?,(()->Void)?){
        return (true,nil,nil)
    }
    
    @objc func popalert() -> Void {
        
        let alert = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        
        for item in companys {
            let sheet = UIAlertAction.init(title: item.contents?["name"]?.rawString(), style: .default) { (action) in
                
                self.companyField.text = item.contents?["name"]?.rawString()
                
                self.selectedCompany = item.contents?["code"]?.rawString() ?? ""
                
                self.load(with: "Detail")
            }
            alert.addAction(sheet)
        }
        let cancel = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
        alert.addAction(cancel)
        self.present(alert, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        load(with: "GetOrg")
        // Do any additional setup after loading the view.
    }

}
